package boun.swe.bob.dao;

import boun.swe.bob.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;


/**
 * This DAO is used for the methods that is using several tables in the database
 */
@Component
public class CompositeDao {

    @Autowired
    UserDao userDao;

    @Autowired
    UserActionDao userActionDao;

    @Autowired
    UserTweetDao userTweetDao;

    @Autowired
    UserFeedDao userFeedDao;

    @Autowired
    UserMessageDao userMessageDao;

    @Autowired
    GroupDao groupDao;

    @Autowired
    Bob_EventDao bob_EventDao;

    @Autowired
    GroupFeedDao groupFeedDao;

    @Autowired
    EventFeedDao eventFeedDao;

    /**
     * This method is used for inserting a new tweet
     *
     * @param user  this variable corresponds to an object with the type of user database
     * @param tweet this variable corresponds to a string containing the users tweet
     */
    public void addUserTweet(User user, String tweet) {

        UserAction userAction = new UserAction();
        userAction.setActionTypeId(1);
        userAction.setUserId(user.getUserId());
        userAction.setActionerUser(userDao.findByPrimaryKey(user.getUserId()));
        userActionDao.save(userAction);
        int actionId = userActionDao.findLastInsertedUserAction(user.getUserId()).getActionId();
        UserTweet userTweet = new UserTweet();
        userTweet.setEntry(tweet);
        userTweet.setActionId(actionId);
        userTweetDao.save(userTweet);
        user.setLatestStatus(tweet);
        userDao.update(user);
    }

    /**
     * This method is used to add the tweet to users' feed
     *
     * @param feedingUserId corresponds to owner of the tweet
     * @param feededUserId  corresponds to destination user that will have the tweet in his/her feed
     * @param feedStr       corresponds to the tweet
     */
    public void addUserFeed(int feedingUserId, int feededUserId, String feedStr) {

        UserAction userAction = new UserAction();
        userAction.setActionTypeId(2);
        userAction.setUserId(feedingUserId);
        userAction.setActionerUser(userDao.findByPrimaryKey(feedingUserId));
        userActionDao.save(userAction);
        int feedingActionId = userActionDao.findLastInsertedUserAction(feedingUserId).getActionId();

        UserFeed feed = new UserFeed();
        feed.setActionId(feedingActionId);
        feed.setEntry(feedStr);
        userFeedDao.save(feed);

        userAction = new UserAction();
        userAction.setActionTypeId(3);
        userAction.setUserId(feededUserId);
        userAction.setActionerUser(userDao.findByPrimaryKey(feedingUserId));
        userActionDao.save(userAction);
        int feededActionId = userActionDao.findLastInsertedUserAction(feededUserId).getActionId();

        feed = new UserFeed();
        feed.setActionId(feededActionId);
        feed.setEntry(feedStr);
        userFeedDao.save(feed);
    }

    /**
     * adds new user message
     *
     * @param fromUserId
     * @param toUserId
     * @param title
     * @param messageBody
     */
    public void addNewMessage(int fromUserId, int toUserId, String title, String messageBody) {
        UserMessage userMsg = new UserMessage();
        userMsg.setFromUserId(fromUserId);
        userMsg.setToUserId(toUserId);
        userMsg.setTitle(title);
        userMsg.setMessageBody(messageBody);
        userMessageDao.save(userMsg);
    }

    /**
     * adds new user submessage
     *
     * @param fromMessageId
     * @param fromUserId
     * @param toUserId
     * @param title
     * @param messageBody
     */
    public void addNewSubMessage(int fromMessageId, int fromUserId, int toUserId, String title, String messageBody) {
        UserMessage userMsg = new UserMessage();
        userMsg.setFromMessageId(fromMessageId);
        userMsg.setFromUserId(fromUserId);
        userMsg.setToUserId(toUserId);
        userMsg.setTitle(title);
        userMsg.setMessageBody(messageBody);
        //userMessageDao.save(userMsg);

    }

    /**
     * add feed to a group
     *
     * @param feedingUserId
     * @param groupId
     * @param feedStr
     */
    public void addGroupFeed(int feedingUserId, int groupId, String feedStr) {

        UserAction userAction = new UserAction();
        userAction.setActionTypeId(4);
        userAction.setUserId(feedingUserId);
        userAction.setActionerUser(userDao.findByPrimaryKey(feedingUserId));
        userActionDao.save(userAction);
        int feedingActionId = userActionDao.findLastInsertedUserAction(feedingUserId).getActionId();

        GroupFeed feed = new GroupFeed();
        feed.setActionId(feedingActionId);
        feed.setGroupId(groupId);
        feed.setEntry(feedStr);
        groupFeedDao.save(feed);
    }


    /**
     * add feed to an event
     * @param feedingUserId
     * @param eventId
     * @param feedStr
     */
    public void addEventFeed(int feedingUserId, int eventId, String feedStr) {

        UserAction userAction = new UserAction();
        userAction.setActionTypeId(5);
        userAction.setUserId(feedingUserId);
        userAction.setActionerUser(userDao.findByPrimaryKey(feedingUserId));
        userActionDao.save(userAction);
        int feedingActionId = userActionDao.findLastInsertedUserAction(feedingUserId).getActionId();

        EventFeed feed = new EventFeed();
        feed.setActionId(feedingActionId);
        feed.setEventId(eventId);
        feed.setEntry(feedStr);
        eventFeedDao.save(feed);
    }

    /**
     * adds a new event
     * @param UserId
     * @param eventName
     * @param eventDescription
     * @param eventLocation
     * @param startDate
     * @param endDate
     */
    public void AddEvent(int UserId, String eventName, String eventDescription, String eventLocation, Timestamp startDate, Timestamp endDate) {
        Bob_Event event = new Bob_Event();
        event.setOwnerUser(userDao.findByPrimaryKey(UserId));
        event.setEventName(eventName);
        event.setDescription(eventDescription);
        event.setLocation(eventLocation);
        event.setStartDate(startDate);
        event.setEndDate(endDate);
        Timestamp createDate = new Timestamp(new Date().getTime());
        event.setCreateDate(createDate);
        bob_EventDao.save(event);
        int EventId = bob_EventDao.findLastInsertedEvent(UserId).getEventId();
        bob_EventDao.JoinEvent(EventId, UserId);
    }

    /**
     * adds a new group
     * @param UserId
     * @param groupName
     * @param groupDescription
     */
    public void AddGroup(int UserId, String groupName, String groupDescription) {
        Group group = new Group();
        group.setOwnerUser(userDao.findByPrimaryKey(UserId));
        group.setGroupName(groupName);
        group.setDescription(groupDescription);
        group.setCreateDate(new Timestamp(new Date().getTime()));
        groupDao.save(group);
        int groupId = groupDao.findLastInsertedGroup(UserId).getGroupId();
        groupDao.JoinGroup(groupId, UserId);

    }


}
